package com.tyqx.hashchainutil.common;

import com.tyqx.hashchainbean.vo.MongoDbUserPathVO;
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.nio.file.Files;
import java.nio.file.Path;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.cert.Certificate;
import java.util.Base64;
import java.util.List;

public class CryptoUtil {

    public String sign(Path clientKeyPath, String message, MongoTemplate mongoTemplate) throws Exception{

        String KEY_ALGORITHM="EC";
        int KEY_SIZE = 256;
        String SIGNATURE_ALGORITHM = "SHA512withECDSA";

        String pathString=clientKeyPath.toString();
        Criteria criteria = Criteria.where("pathString").is(pathString);
        Query query = Query.query(criteria);
        byte[] clientKeyBytes=null;
        String collName="UserPath";
        try {
            List<MongoDbUserPathVO> mongoDbUserPathVOList = mongoTemplate.find(query, MongoDbUserPathVO.class, collName);
            if(mongoDbUserPathVOList.size()>0){
                clientKeyBytes=mongoDbUserPathVOList.get(0).getFileBytes();
            }else{
                clientKeyBytes = Files.readAllBytes(clientKeyPath);
                MongoDbUserPathVO mongoDbUserPathVO=new MongoDbUserPathVO();
                mongoDbUserPathVO.setPathString(pathString);
                mongoDbUserPathVO.setFileBytes(clientKeyBytes);
                mongoTemplate.insert(mongoDbUserPathVO,collName);
            }
        }catch (Exception e){
            clientKeyBytes = Files.readAllBytes(clientKeyPath);
            MongoDbUserPathVO mongoDbUserPathVO=new MongoDbUserPathVO();
            mongoDbUserPathVO.setPathString(pathString);
            mongoDbUserPathVO.setFileBytes(clientKeyBytes);
            mongoTemplate.insert(mongoDbUserPathVO,collName);
        }

        //byte[] clientKeyBytes = Files.readAllBytes(clientKeyPath.toFile().toPath());

        CryptoPrimitives suite = new CryptoPrimitives();
        suite.init();

        PrivateKey privateKey = suite.bytesToPrivateKey(clientKeyBytes);

        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
        signature.initSign(privateKey);
        signature.update(message.getBytes());

        return Base64.getEncoder().encodeToString(signature.sign());
    }

    public String sign(Path clientKeyPath, String message) throws Exception{

        String KEY_ALGORITHM="EC";
        int KEY_SIZE = 256;
        String SIGNATURE_ALGORITHM = "SHA512withECDSA";

       /* Criteria criteria = Criteria.where("path").is(attachmentHashValue);
        Query query = Query.query(criteria);
        List<ImageVO> imageVO = mongoTemplate.find(query, ImageVO.class, imageQueryParamVO.getCollectionName());*/
        byte[] clientKeyBytes = Files.readAllBytes(clientKeyPath.toFile().toPath());

        CryptoPrimitives suite = new CryptoPrimitives();
        suite.init();

        PrivateKey privateKey = suite.bytesToPrivateKey(clientKeyBytes);

        Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
        signature.initSign(privateKey);
        signature.update(message.getBytes());

        return Base64.getEncoder().encodeToString(signature.sign());
    }

    public boolean verify(Path clientCertPath, String signStr, String message) throws Exception{

        return false;
    }

    public boolean verify(Path clientCertPath, String signStr, String message,MongoTemplate mongoTemplate) throws Exception{

       return true;
    }
}
